package top.carrotguo.bean.linklist;

/**
 * 双向链表结点
 * @Author: Carrot
 * @Mail: 1053155183carrot@gmail.com
 * @Date: Created in 19:20 2018-05-29
 */
public class LinkNode<T> {
    public T value;             //链表元素值
    public LinkNode<T> preNode; //结点前驱
    public LinkNode<T> nextNode;//结点后继

    /**
     * 结点前插入
     * @param e   结点内元素的值
     */
    public LinkNode<T> insertAsPred(T e){
        LinkNode<T> pre = new LinkNode<T>();
        pre.value = e;
        pre.nextNode = this;
        pre.preNode = this.preNode;
        this.preNode.nextNode = pre;
        this.preNode = pre;
        return pre;
    }

    /**
     * 结点后插入
     * @param e     结点内元素的值
     */
    public LinkNode<T> insertAsSucc(T e){
        LinkNode<T> succ = new LinkNode<T>();
        succ.value = e;
        succ.preNode = this;
        succ.nextNode = this.nextNode;
        this.nextNode.preNode = succ;
        this.nextNode = succ;
        return succ;
    }
}
